/*******************************************************************************
 * Copyright 2011-2013 Sergey Tarasevich
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package com.yeook.ymax.ui.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.widget.AbsListView;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.nostra13.universalimageloader.core.assist.PauseOnScrollListener;
import com.yeook.ymax.R;

/**
 * 
 * 
 * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
 */
public class AbsListViewBaseActivity extends ImageBaseActivity {

	protected static final String STATE_PAUSE_ON_SCROLL = "STATE_PAUSE_ON_SCROLL";
	protected static final String STATE_PAUSE_ON_FLING = "STATE_PAUSE_ON_FLING";

	public AbsListView listView;

	protected boolean pauseOnScroll = false;
	protected boolean pauseOnFling = true;

	@Override
	public void onRestoreInstanceState(Bundle savedInstanceState) {
		pauseOnScroll = savedInstanceState.getBoolean(STATE_PAUSE_ON_SCROLL, false);
		pauseOnFling = savedInstanceState.getBoolean(STATE_PAUSE_ON_FLING, true);
	}

	@Override
	public void onResume() {
		super.onResume();
		applyScrollListener();
	}

	private void applyScrollListener() {
		listView.setOnScrollListener(new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling));
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		outState.putBoolean(STATE_PAUSE_ON_SCROLL, pauseOnScroll);
		outState.putBoolean(STATE_PAUSE_ON_FLING, pauseOnFling);
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
//		MenuItem pauseOnScrollItem = menu.findItem(R.id.item_pause_on_scroll);
//		pauseOnScrollItem.setVisible(true);
//		pauseOnScrollItem.setChecked(pauseOnScroll);
//
//		MenuItem pauseOnFlingItem = menu.findItem(R.id.item_pause_on_fling);
//		pauseOnFlingItem.setVisible(true);
//		pauseOnFlingItem.setChecked(pauseOnFling);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent = null;
		switch (item.getItemId()) {
//			case R.id.item_pause_on_scroll:
//				pauseOnScroll = !pauseOnScroll;
//				item.setChecked(pauseOnScroll);
//				applyScrollListener();
//				return true;
//			case R.id.item_pause_on_fling:
//				pauseOnFling = !pauseOnFling;
//				item.setChecked(pauseOnFling);
//				applyScrollListener();
//				return true;
			case R.id.setting_config:
				intent = new Intent();
				intent.setClass(this, SettingsActivity.class);
				startActivity(intent);
				return true;
			case R.id.page_ringtone:
				if(!(this instanceof RingtoneUIActivity)){
					intent = new Intent();
					intent.setClass(this, RingtoneUIActivity.class);
					startActivity(intent);
				}
				return true;
			case R.id.page_wallaper:
				if(!(this instanceof ResponsiveUIActivity)){
					intent = new Intent();
					intent.setClass(this, ResponsiveUIActivity.class);
					startActivity(intent);
				}
				return true;
			default:
				return super.onOptionsItemSelected(item);
		}
	}
}
